package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.context.Contexts;
import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.RolePermissionDto;
import com.cardone.platform.authority.service.RolePermissionService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 角色与许可
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/rolePermission/")
@Controller
public class RolePermissionController {

	/**
	 * 角色与许可：删除
	 * 
	 * @param deleteRolePermission
	 *            角色与许可对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
  public Callable<ReturnDataDto<String>> deleteJson(final RolePermissionDto deleteRolePermission) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RolePermissionService.class).deleteByIds(deleteRolePermission);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 角色与许可：编辑
	 * 
	 * @param findRolePermission
	 *            角色与许可对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<RolePermissionDto>> findByIdJson(final RolePermissionDto findRolePermission, final Model model) {
		return new Callable<ReturnDataDto<RolePermissionDto>>() {
			@Override
			public ReturnDataDto<RolePermissionDto> call() throws Exception {
				try {
					final RolePermissionDto rolePermission = ContextHolder.getBean(RolePermissionService.class).findById(RolePermissionDto.class, findRolePermission);

					return ReturnDataUtils.newReturnData(rolePermission);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RolePermissionController.log);
				}
			}
		};
	}

	/**
	 * 角色与许可：分页查询
	 * 
	 * @param paginationRolePermission
	 *            角色与许可对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
  public Callable<ReturnDataDto<PaginationDto<RolePermissionDto>>> paginationByLikeCodeJson(final RolePermissionDto paginationRolePermission) {
		return new Callable<ReturnDataDto<PaginationDto<RolePermissionDto>>>() {
			public ReturnDataDto<PaginationDto<RolePermissionDto>> call() throws Exception {
				try {
					final PaginationDto<RolePermissionDto> rolePermissionPagination = ContextHolder.getBean(RolePermissionService.class).paginationByLikeCode(RolePermissionDto.class, paginationRolePermission);
				
					return ReturnDataUtils.newReturnData(rolePermissionPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 角色与许可：分页查询
	 * 
	 * @param paginationRolePermission
	 *            角色与许可对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
  public void paginationHtml(final RolePermissionDto paginationRolePermission, final Model model) {
		final PaginationDto<RolePermissionDto> rolePermissionPagination = ContextHolder.getBean(RolePermissionService.class).paginationByLikeCode(RolePermissionDto.class, paginationRolePermission);
		
		model.addAttribute("rolePermissionPagination", rolePermissionPagination);
	}
	
	/**
	 * 角色与许可：分页查询
	 * 
	 * @param findRolePermission
	 *            角色与许可对象
	 * @param model
	 *            上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
  public void editHtml(final RolePermissionDto findRolePermission, final Model model) {
		final RolePermissionDto rolePermission = ContextHolder.getBean(RolePermissionService.class).findById(RolePermissionDto.class, findRolePermission);
		
		model.addAttribute("rolePermission", rolePermission);
	}

	/**
	 * 角色与许可：查询
	 * 
	 * @param findListRolePermission
	 *            角色与许可对象
	 * @return 角色与许可对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
  	public Callable<ReturnDataDto<List<RolePermissionDto>>> findListByLikeCodeJson(final RolePermissionDto findListRolePermission) {
		return new Callable<ReturnDataDto<List<RolePermissionDto>>>() {
			public ReturnDataDto<List<RolePermissionDto>> call() throws Exception {
				try {
					final List<RolePermissionDto> rolePermissionList = ContextHolder.getBean(RolePermissionService.class).findListByLikeCode(RolePermissionDto.class, findListRolePermission);
				
					return ReturnDataUtils.newReturnData(rolePermissionList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}

	/**
	 * 角色与许可：保存
	 * 
	 * @param saveRolePermission
	 *            角色与许可对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
  public Callable<ReturnDataDto<String>> saveJson(final RolePermissionDto saveRolePermission) {
		return new Callable<ReturnDataDto<String>>() {
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RolePermissionService.class).saveByIdOrCode(RolePermissionDto.class, saveRolePermission);
					
					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, log);
				}
			}
		};
	}
}
